namespace Billing2
{
    using AjaxControlToolkit;
    using System;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Microsoft.Security.Application;

    public class ucBillCustomer : UserControl
    {
        protected UpdatePanel ActionPanel;
        protected Button btnAddCostCentre;
        protected Button btnDelete;
        protected ConfirmButtonExtender btnDelete_ConfirmButtonExtender;
        protected Button btnRemoveListItem;
        protected Button btnSave;
        protected DropDownList ddlGroupBy;
        protected DropDownList ddlOrganization;
        protected DropDownList ddlSite;
        private DataTable dt;
        protected Label lblCostCentre;
        protected Label lblCostCentreAvail;
        protected ListBox lstboxCostCentre;
        protected ListBox lstboxCostCentreAvail;
        protected Panel PanelDetails;
        protected Timer TimerCloseIt;
        protected TextBox txtAddress;
        protected TextBox txtBUCode;
        protected TextBox txtContactPerson;
        protected TextBox txtCustName1;
        protected TextBox txtCustName2;
        protected TextBox txtCustNo;
        protected TextBox txtEmailTo;
        protected TextBox txtFaxNo;
        protected TextBox txtRemarks;
        protected TextBox txtSpecialInstruction;
        protected TextBox txtTelNo;
        protected ucHighlight ucHighlight1;
        protected UpdatePanel updatePanel3;
        protected UpdateProgress updateprogress1;
        protected HiddenField xDialogID;
        protected HiddenField xPrimaryKey;

        public void BindAvailableCostCentre()
        {
            this.lstboxCostCentreAvail.Items.Clear();
            if (this.ddlSite.SelectedIndex > 0)
            {
                this.lstboxCostCentreAvail.DataSource = new ClsCostCentresInSite { SiteID = Convert.ToInt64(this.ddlSite.SelectedValue) }.SelectBySiteID();
                this.lstboxCostCentreAvail.DataTextField = "CostCentreIDName";
                this.lstboxCostCentreAvail.DataValueField = "ID";
                this.lstboxCostCentreAvail.DataBind();
            }
        }

        public void BindEditData()
        {
            if ((this.dt != null) && (this.dt.Rows.Count > 0))
            {
                this.xPrimaryKey.Value = Common.EscapeInteger(this.dt.Rows[0]["ID"].ToString());
                this.txtCustNo.Text = this.dt.Rows[0]["CustNo"].ToString();
                this.ViewState["CustNo"] = this.dt.Rows[0]["CustNo"].ToString();
                this.txtCustName1.Text = this.dt.Rows[0]["CustName1"].ToString();
                this.txtCustName2.Text = this.dt.Rows[0]["CustName2"].ToString();
                this.txtBUCode.Text = this.dt.Rows[0]["BUCode"].ToString();
                this.txtAddress.Text = this.dt.Rows[0]["Address"].ToString();
                this.txtContactPerson.Text = this.dt.Rows[0]["ContactPerson"].ToString();
                this.txtSpecialInstruction.Text = this.dt.Rows[0]["SpecialInstruction"].ToString();
                this.txtContactPerson.Text = this.dt.Rows[0]["ContactPerson"].ToString();
                this.ddlGroupBy.SelectedIndex = this.ddlGroupBy.Items.IndexOf(this.ddlGroupBy.Items.FindByValue(this.dt.Rows[0]["GroupBy"].ToString()));
                this.txtTelNo.Text = this.dt.Rows[0]["TelNo"].ToString();
                this.txtFaxNo.Text = this.dt.Rows[0]["FaxNo"].ToString();
                this.txtEmailTo.Text = this.dt.Rows[0]["EmailTo"].ToString();
                this.txtRemarks.Text = this.dt.Rows[0]["Remarks"].ToString();
                this.OrganizationID = this.dt.Rows[0]["OrgID"].ToString();
                this.SiteID = this.dt.Rows[0]["SiteID"].ToString();
                this.SetVisibleCostCentreRelatedComponent();
                this.lstboxCostCentre.Items.Clear();
                DataTable dtBcc = new ClsBillCustomerCC { BillCustomerID = Convert.ToInt64(this.xPrimaryKey.Value) }.SelectByBillCustomerID();
                if (dtBcc != null)
                {
                    for (int i = 0; i < dtBcc.Rows.Count; i++)
                    {
                        this.lstboxCostCentre.Items.Add(new ListItem(
                            Common.EscapeCostCenterName(dtBcc.Rows[i]["CostCentreIDName"].ToString()),
                            Common.EscapeInteger(dtBcc.Rows[i]["CostCentreID"].ToString())));
                    }
                }
            }
        }

        private void BindOrganization()
        {
            DataTable dt1 = new ClsOrganization().SelectAll();
            this.ddlOrganization.Items.Clear();
            this.ddlOrganization.Items.Add(new ListItem("Select Organization", "0"));
            for (int i = 0; i < dt1.Rows.Count; i++)
            {
                this.ddlOrganization.Items.Add(
                    new ListItem(
                        Common.EscapeCostCenterName(dt1.Rows[i]["OrgIDName"].ToString()),
                        Common.EscapeInteger(dt1.Rows[i]["ID"].ToString())));
            }
            this.ddlOrganization.DataBind();
            this.BindSite();
        }

        public void BindSite()
        {
            this.ddlSite.Items.Clear();
            this.ddlSite.Items.Add(new ListItem("Select Site", "0"));
            if (this.ddlOrganization.SelectedIndex > 0)
            {
                DataTable dt1 = new ClsSitesInOrganization { OrgID = Convert.ToInt64(this.ddlOrganization.SelectedValue) }.SelectByOrgID();
                for (int i = 0; i < dt1.Rows.Count; i++)
                {
                    this.ddlSite.Items.Add(
                        new ListItem(
                            Common.EscapeCostCenterName(dt1.Rows[i]["SiteIDName"].ToString()),
                            Common.EscapeInteger(dt1.Rows[i]["ID"].ToString())));
                }
                this.ddlSite.DataBind();
                this.BindAvailableCostCentre();
            }
        }

        protected void btnAddCostCentre_Click(object sender, EventArgs e)
        {
            ClsUtilityHelper.TransferListboxItem(this.lstboxCostCentreAvail, this.lstboxCostCentre);
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            ClsBillCustomer cls = new ClsBillCustomer();
            if (Convert.ToInt64(this.xPrimaryKey.Value) > 0)
            {
                cls.ID = Convert.ToInt64(this.xPrimaryKey.Value);
                cls.Delete();
                this.ucHighlight1.Message = "Record Deleted";
                this.lstboxCostCentre.Items.Clear();
                this.lstboxCostCentreAvail.Items.Clear();
                ClsUtilityHelper.ResetFormControlValues(this);
                this.TimerCloseIt.Enabled = true;
            }
        }

        protected void btnRemoveListItem_Click(object sender, EventArgs e)
        {
            ClsUtilityHelper.RemoveListboxSelectedItem(this.lstboxCostCentre);
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (this.txtCustNo.Text.Trim() == "")
            {
                this.ucHighlight1.ErrorMessage = "Invalid Customer No";
            }
            else
            {
                if (this.txtEmailTo.Text.Trim() != "")
                {
                    string strData = this.txtEmailTo.Text;
                    char[] separator = new char[] { ',', ';' };
                    string[] strSplitArr = strData.Split(separator);
                    foreach (string arrStr in strSplitArr)
                    {
                        if (!(arrStr.Trim() == "" || ClsValidationHelper.IsEmail(arrStr.Trim())))
                        {
                            this.ucHighlight1.ErrorMessage = "Invalid Email Address";
                            return;
                        }
                    }
                }
                if (this.ddlGroupBy.SelectedIndex == -1)
                {
                    this.ucHighlight1.ErrorMessage = "Invalid Group By";
                }
                else if (this.ddlOrganization.SelectedIndex <= 0)
                {
                    this.ucHighlight1.ErrorMessage = "Invalid Organization";
                }
                else if (this.ddlSite.SelectedIndex <= 0)
                {
                    this.ucHighlight1.ErrorMessage = "Invalid Site";
                }
                else if ((this.ddlGroupBy.SelectedValue == "CostCentre") && (this.lstboxCostCentre.Items.Count == 0))
                {
                    this.ucHighlight1.ErrorMessage = "Invalid Cost Centre. You have to select at least one Cost Centre";
                }
                else
                {
                    bool flag = ClsValidationHelper.IsRecordExist("BillCustomer", "CustNo", this.txtCustNo.Text);
                    if (flag && (Convert.ToInt64(this.xPrimaryKey.Value) == 0))
                    {
                        this.ucHighlight1.ErrorMessage = "Record Exists. Customer No must be unique";
                    }
                    else if ((flag && (Convert.ToInt64(this.xPrimaryKey.Value) > 0)) && (this.ViewState["CustNo"].ToString().ToUpper() != this.txtCustNo.Text.ToUpper()))
                    {
                        this.ucHighlight1.ErrorMessage = "Record Exists. Customer No must be unique";
                    }
                    else
                    {
                        int i;
                        ClsBillCustomer cls = new ClsBillCustomer {
                            CustNo = this.txtCustNo.Text,
                            BillType = "",
                            CustName1 = this.txtCustName1.Text,
                            CustName2 = this.txtCustName2.Text,
                            BUCode = this.txtBUCode.Text,
                            Address = this.txtAddress.Text,
                            ContactPerson = this.txtContactPerson.Text,
                            PaymentTerm = "",
                            SpecialInstruction = this.txtSpecialInstruction.Text,
                            GroupBy = this.ddlGroupBy.SelectedValue,
                            SiteID = Convert.ToInt64(this.SiteID),
                            TelNo = this.txtTelNo.Text,
                            FaxNo = this.txtFaxNo.Text,
                            EmailTo = this.txtEmailTo.Text,
                            Remarks = this.txtRemarks.Text
                        };
                        if (Convert.ToInt64(this.xPrimaryKey.Value) > 0)
                        {
                            cls.ID = Convert.ToInt64(this.xPrimaryKey.Value);
                            cls.ModifiedBy = LoginUser.UserID;
                            cls.Update();
                            ClsBillCustomerCC clsBcc = new ClsBillCustomerCC {
                                BillCustomerID = cls.ID
                            };
                            clsBcc.DeleteByBillCustomerID();
                            if (this.ddlGroupBy.SelectedValue == "CostCentre")
                            {
                                for (i = 0; i < this.lstboxCostCentre.Items.Count; i++)
                                {
                                    clsBcc.CostCentreID = Convert.ToInt64(this.lstboxCostCentre.Items[i].Value);
                                    clsBcc.Save();
                                }
                            }
                            this.ucHighlight1.Message = " Record Updated";
                        }
                        else
                        {
                            cls.CreatedBy = LoginUser.UserID;
                            long BillCustomerID = cls.Save();
                            if (this.ddlGroupBy.SelectedValue == "CostCentre")
                            {
                                for (i = 0; i < this.lstboxCostCentre.Items.Count; i++)
                                {
                                    new ClsBillCustomerCC { BillCustomerID = BillCustomerID, CostCentreID = Convert.ToInt64(this.lstboxCostCentre.Items[i].Value) }.Save();
                                }
                            }
                            this.ucHighlight1.Message = " Record Saved";
                        }
                        this.lstboxCostCentre.Items.Clear();
                        this.lstboxCostCentreAvail.Items.Clear();
                        ClsUtilityHelper.ResetFormControlValues(this);
                        this.BindOrganization();
                        this.SetVisibleCostCentreRelatedComponent();
                        //if (Convert.ToInt64(this.xPrimaryKey.Value) > 0)
                        //{
                            this.TimerCloseIt.Enabled = true;
                        //}
                    }
                }
            }
        }

        protected void ddlGroupBy_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.SetVisibleCostCentreRelatedComponent();
        }

        protected void ddlOrganization_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.BindSite();
        }

        protected void ddlSite_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.BindAvailableCostCentre();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                this.BindOrganization();
            }
            this.ucHighlight1.CloseAll();
        }

        private void SetVisibleCostCentreRelatedComponent()
        {
            bool xVisible = false;
            if (this.ddlGroupBy.SelectedIndex != -1)
            {
                if (this.ddlGroupBy.SelectedValue == "Site")
                {
                    xVisible = false;
                }
                else
                {
                    xVisible = true;
                }
            }
            this.lblCostCentre.Visible = xVisible;
            this.lblCostCentreAvail.Visible = xVisible;
            this.lstboxCostCentre.Visible = xVisible;
            this.lstboxCostCentreAvail.Visible = xVisible;
            this.btnRemoveListItem.Visible = xVisible;
            this.btnAddCostCentre.Visible = xVisible;
        }

        protected void TimerCloseIt_Tick(object sender, EventArgs e)
        {
            ClsUtilityHelper.CloseJQueryDialog(this.DialogName, this);
            this.TimerCloseIt.Enabled = false;
            ClsUtilityHelper.EnableFormControlValues(this);
            this.ucHighlight1.CloseAll();
        }

        public bool Create0
        {
            get
            {
                return this.btnSave.Visible;
            }
            set
            {
                this.btnSave.Visible = value;
            }
        }

        public bool Delete0
        {
            get
            {
                return this.btnDelete.Visible;
            }
            set
            {
                this.btnDelete.Visible = value;
            }
        }

        public string DialogName
        {
            get
            {
                return this.xDialogID.Value;
            }
            set
            {
                this.xDialogID.Value = value;
            }
        }

        public DataTable EditData
        {
            get
            {
                return this.dt;
            }
            set
            {
                this.dt = value;
                this.BindEditData();
                this.TimerCloseIt.Enabled = false;
            }
        }

        public string OrganizationID
        {
            get
            {
                if (this.ddlOrganization.SelectedIndex == -1)
                {
                    return "0";
                }
                return this.ddlOrganization.SelectedValue;
            }
            set
            {
                this.ddlOrganization.SelectedIndex = -1;
                if ((value != "") && (value != "0"))
                {
                    this.ddlOrganization.Items.FindByValue(value).Selected = true;
                }
                this.BindSite();
            }
        }

        public string SiteID
        {
            get
            {
                if (this.ddlSite.SelectedIndex == -1)
                {
                    return "0";
                }
                return this.ddlSite.SelectedValue;
            }
            set
            {
                this.ddlSite.ClearSelection();
                if ((value != "") && (value != "0"))
                {
                    this.ddlSite.SelectedIndex = this.ddlSite.Items.IndexOf(this.ddlSite.Items.FindByValue(value));
                }
                this.BindAvailableCostCentre();
            }
        }

        public bool Update0
        {
            get
            {
                return this.btnSave.Visible;
            }
            set
            {
                this.btnSave.Visible = value;
            }
        }
    }
}
